/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * FrmTest.java
 *
 * Created on 17-may-2009, 3:40:07
 */
package gui.forms;

import gui.tools.MyCanvas;
import gui.tools.ViewType;
import java.awt.Color;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import lib.simul.Simulation;

/**
 *
 * @author jorge
 */
public class FrmResults extends javax.swing.JFrame {

    private MyCanvas cv;
    private Simulation simulation;
    private JMenuItem jmi;

    /** Creates new form FrmResults */
    public FrmResults(Simulation simulation) {
        initComponents();

        this.getRootPane().setDefaultButton(btnClose);
        String fileString = "/res/icons/logo.png";
        try {
            Image icon = ImageIO.read(getClass().getResource(fileString));
            this.setIconImage(icon);
        } catch (java.io.IOException ioe) {
            System.out.println("Error loading file: " + fileString);
        }

        rbItems.setSelected(true);
        ButtonGroup btng = new ButtonGroup();
        btng.add(rbItems);
        btng.add(rbStations);
        this.setTitle("Result viewer");
        this.simulation = simulation;

        int simuls = simulation.getResults().size();
        for (int i = 0; i < simuls; i++) {
            cbShow.addItem("Simulation " + (i + 1));
        }

        cv = new MyCanvas(simulation);
        cv.setLocation(10, 50);
        add(cv);
        ppName = new JPopupMenu();
        jmi = new JMenuItem("");
        ppName.add(jmi);
        cv.addMouseListener(new java.awt.event.MouseAdapter() {

            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                String name = cv.getText(evt.getX(), evt.getY());
                if (name != null) {
                    jmi.setText(name);
                    ppName.show(cv, evt.getX(), evt.getY());
                }
            }
        });
        cv.setBackground(Color.black);
        this.setExtendedState(MAXIMIZED_BOTH);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ppName = new javax.swing.JPopupMenu();
        lblShow = new javax.swing.JLabel();
        rbStations = new javax.swing.JRadioButton();
        rbItems = new javax.swing.JRadioButton();
        lblView = new javax.swing.JLabel();
        cbShow = new javax.swing.JComboBox();
        btnViewEL = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();

        setExtendedState(MAXIMIZED_BOTH);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        lblShow.setText("Show result:");

        rbStations.setText("Stations");
        rbStations.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbStationsActionPerformed(evt);
            }
        });

        rbItems.setText("Items");
        rbItems.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbItemsActionPerformed(evt);
            }
        });

        lblView.setText("View based on:");

        cbShow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbShowActionPerformed(evt);
            }
        });

        btnViewEL.setText("View event list...");
        btnViewEL.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewELActionPerformed(evt);
            }
        });

        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblView)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rbItems)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(rbStations)
                .addGap(18, 18, 18)
                .addComponent(lblShow)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cbShow, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnViewEL)
                .addGap(349, 349, 349))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(1103, Short.MAX_VALUE)
                .addComponent(btnClose)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblShow)
                    .addComponent(rbStations)
                    .addComponent(rbItems)
                    .addComponent(lblView)
                    .addComponent(cbShow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnViewEL))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 514, Short.MAX_VALUE)
                .addComponent(btnClose)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-1194)/2, (screenSize.height-618)/2, 1194, 618);
    }// </editor-fold>//GEN-END:initComponents

    private void cbShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbShowActionPerformed
        Show();
    }//GEN-LAST:event_cbShowActionPerformed

    private void rbItemsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbItemsActionPerformed
        Show();
    }//GEN-LAST:event_rbItemsActionPerformed

    private void rbStationsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbStationsActionPerformed
        Show();
    }//GEN-LAST:event_rbStationsActionPerformed

    private void btnViewELActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewELActionPerformed
        int num = cbShow.getSelectedIndex();
        new FrmEventList(simulation.getResults().get(num)).setVisible(true);
    }//GEN-LAST:event_btnViewELActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnCloseActionPerformed

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        Show();
    }//GEN-LAST:event_formComponentResized

    private void Show() {
        if (cv != null) {
            int num = cbShow.getSelectedIndex();
            ViewType view = ViewType.ITEMS;
            if (rbStations.isSelected()) {
                view = ViewType.STATIONS;
            }
            cv.Show(num, getWidth() - 20, getHeight() - 125, view);
        }

    }

    
    @Override
    public void setVisible(boolean b) {
        super.setVisible(b);

        this.setExtendedState(MAXIMIZED_BOTH);
        Show();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnViewEL;
    private javax.swing.JComboBox cbShow;
    private javax.swing.JLabel lblShow;
    private javax.swing.JLabel lblView;
    private javax.swing.JPopupMenu ppName;
    private javax.swing.JRadioButton rbItems;
    private javax.swing.JRadioButton rbStations;
    // End of variables declaration//GEN-END:variables
}
